/**
 * 
 */
package com.nari.baseinfo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.ICityDao;
import com.nari.baseinfo.dao.ICommunityDao;
import com.nari.baseinfo.model.ICommunity;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class CommunityServiceImpl extends
		GenericServiceImpl<ICommunity, String> implements ICommunityService {
	private ICityDao iCityDao;

	/**
	 * 根据小区所属信息（所属三级机构、所属项目等）查询小区列表
	 * @param provinceCityID 所属省级机构
	 * @param capitalCityID 所属市级机构
	 * @param districtCityID 所属区级机构
	 * @param projectUID 所属项目
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-25
	 */
	public List<ICommunity> queryCommunitysByBelongings(String provinceCityID,
			String capitalCityID, String districtCityID, String projectUID) throws Exception {
		ICommunityDao iCommunityDao = (ICommunityDao) this.getiGenericDao();
		return iCommunityDao.queryCommunitysByBelongings(provinceCityID, capitalCityID, districtCityID, projectUID);
	}

	/**
	 * 分页查询小区信息
	 * @param communityName 小区名称(可选)
	 * @param provinceCityID 所属省级机构(可选)
	 * @param capitalCityID 所属市级机构(可选)
	 * @param districtCityID 所属区级机构(可选)
	 * @param projectUID 所属项目(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-25
	 */
	public ResultWithPageCond<ICommunity> queryCommunityWithPageCond(String communityName, 
			String provinceCityID, String capitalCityID, String districtCityID, 
			String projectUID, PageCond pageCond) throws Exception {
		ICommunityDao iCommunityDao = (ICommunityDao) this.getiGenericDao();
		return iCommunityDao.queryCommunityWithPageCond(communityName, provinceCityID,
				capitalCityID, districtCityID, projectUID, pageCond);
	}

	/**
	 * 通过小区列表查询与之关联的行政机构ID-机构名称映射表
	 * @param projectList 小区列表，若列表为空，则返回空的映射表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public Map<String, String> queryReferCitysByCommunityList(List<ICommunity> communityList) 
			throws Exception {
		List<String> cityIDList = null;
		if (communityList != null && communityList.size() > 0) {
			cityIDList = new ArrayList<String>();
			for (ICommunity community : communityList) {
				cityIDList.add(community.getProvinceCityID());
				cityIDList.add(community.getCapitalCityID());
				cityIDList.add(community.getDistrictCityID());
			}
		}
		return iCityDao.queryIdNameMapByIDList(cityIDList);
	}

	/**
	 * 通过小区ID列表查询小区信息
	 * @param idList 小区ID列表，若列表为空，则返回空列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-13
	 */
	public List<ICommunity> queryCommunityByIDList(List<String> idList) throws Exception {
		ICommunityDao iCommunityDao = (ICommunityDao) this.getiGenericDao();
		return iCommunityDao.queryCommunityByIDList(idList);
	}
	
	/**
	 * 通过小区所属关系统计小区的个数
	 * @param provinceCityUID 所属省级机构(可选)
	 * @param capitalCityUID 所属市级机构(可选)
	 * @param districtCityUID 所属区级机构(可选)
	 * @param projectUID 所属项目(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-21
	 */
	public int countCommunitySumByBelongs(String provinceCityUID, String capitalCityUID, 
			String districtCityUID, String projectUID) throws Exception {
		ICommunityDao iCommunityDao = (ICommunityDao) this.getiGenericDao();
		return iCommunityDao.countCommunitySumByBelongs(provinceCityUID, capitalCityUID, districtCityUID, projectUID);
	}
	
	/**
	 * 根据小区ID级联删除关联信息
	 * 包括：小区、楼栋、单元、热用户、系统用户及角色、集中器、热量表、分配表、散热器、
	 * 分摊域、分摊域分摊账单、用户分摊账单
	 * 注意：热量表数据、分配表数据等表计数据具有不可恢复性，因此不能作为级联删除的对象
	 * @param communityUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-20
	 */
	public ResponseMsg deleteCascadeByCommunityUID(String communityUID) throws Exception {
		ICommunityDao iCommunityDao = (ICommunityDao) this.getiGenericDao();
		return iCommunityDao.deleteCascadeByCommunityUID(communityUID);
	}

	/**
	 * @return the iCityDao
	 */
	public ICityDao getiCityDao() {
		return iCityDao;
	}

	/**
	 * @param iCityDao
	 *            the iCityDao to set
	 */
	public void setiCityDao(ICityDao iCityDao) {
		this.iCityDao = iCityDao;
	}

}
